function gettoday() {
    var myDate = new Date();
    var Dt;
    Dt = myDate.getFullYear() + '-';
    Dt += myDate.getMonth() + 1 + '-';
    Dt += myDate.getDate();
    return Dt;
}

function RefreshList(gd, beginday, endday) {
    api.setRefreshHeaderInfo({
        loadingImg: 'widget://image/refresh.png',
        bgColor: '#ccc',
        textColor: '#fff',
        textDown: '下拉刷新...',
        textUp: '松开刷新...'
    }, function(ret, err) {
        var ul = $api.byId('list');
        $('#list li').each(function() {
            $(this).remove();
        });
        listKaoQin(gd, beginday, endday);
        setTimeout(function() {
            api.refreshHeaderLoadDone();
        }, 1000);
    });
}

function refreshlist(gd, beginday, endday) {
    var ul = $api.byId('list');
    $('#list li').each(function() {
        $(this).remove();
    });
    listKaoQin(gd, beginday, endday);
}

function expandlist(index, clickitem) {
    var toolbarlist = document.getElementsByClassName('reason');
    var toolbarimg = document.getElementsByClassName('toobarimg');
    var clicktoolitem = toolbarlist[index];
    var click = clicktoolitem.getAttribute("data-click");

    // 复原其他tool，一次只打开一个tool
    for (var i = 0; i < toolbarlist.length; i++) {
        toolbarlist[i].setAttribute("data-click", 0);
        toolbarlist[i].style.display = 'none';
        toolbarimg[i].src = "../../icon/unfold.png";
    }
    if (click == 0) {
        // 点开
        clicktoolitem.setAttribute("data-click", 1);
        clickitem.src = "../../icon/packup.png";
        clicktoolitem.style.display = '-webkit-box';
    } else {
        // 关闭
        clicktoolitem.setAttribute("data-click", 0);
        clickitem.src = "../../icon/unfold.png";
        clicktoolitem.style.display = 'none';
    }
}
var phones = [];

function listKaoQin(gd, beginday, endday) {
    config = getConfig();
    var k = 0;
    //alert("test" + day.getbeginday() + "    " + day.getendday());
    api.ajax({
        url: 'http://' + config.serverIp + '/XSKQ/xskq/listKaoQin.do',
        method: 'post',
        data: {
            values: {
                "Q_EQL_clazz.grade.oid": gd,
                "Q_GED_cdate": beginday,
                "Q_LED_cdate": endday
            }
        },
        headers: {
            "content-type": "application/x-www-form-urlencoded",
        }
    }, function(ret, err) {
        if (ret) {

            var ul = $api.byId('list');
            if (ret.result.length == 0) {
                var html = '<li class="aui-list-item aui-list-item-middle">';
                html += '<div class="aui-media-list-item-inner">';
                html += '<div class="aui-list-item-inner">';
                html += '<div class="aui-list-item-text">';
                html += '<div class="aui-list-item-title aui-font-size-16" style="margin:0 auto">' + '今日暂无考勤信息' + '</div>';
                html += '</div>';
                html += '</div>';
                html += '</div>';
                html += '</li>';
                $api.append(ul, html);
            } else {
                for (var i = 0; i < ret.result.length; i++) {
                    //ontouchstart="gtouchstart(this)" ontouchmove="gtouchmove()" ontouchend="gtouchend()"
                    var obj = ret.result[i];
                    var html = '<li class="aui-list-item aui-list-item-middle" >';
                    html += '<div class="aui-media-list-item-inner">';
                    html += '<div class="aui-list-item-inner">';
                    html += '<div class="aui-list-item-text">';
                    if (obj.allCome == 1) {
                        html += '<div class="aui-list-item-title aui-font-size-16">' + obj.clazz.name + '</div>';
                        html += '<div>' + obj.cdate + '</div>';
                        html += '</div>';
                        html += '<table>';
                        html += '<tr>';
                        html += '<td class="item">' + obj.jieciType.itemValue + '</td>';
                        html += '<td class="item">' + obj.clazz.master.fullname + '</td>';
                        html += '<td class="item" style="color:#008000">' + '全勤' + '</td>';
                    } else {
                        phones[k] = obj.student.phone;
                        html += '<div class="aui-list-item-title aui-font-size-16">' + obj.student.name + '</div>';
                        html += '<div>' + obj.cdate + '</div>';
                        html += '</div>';
                        html += '<table>';
                        html += '<tr>';
                        html += '<td class="item">' + obj.clazz.name + '</td>';
                        html += '<td class="item">' + obj.clazz.master.fullname + '</td>';
                        if (obj.reason == null) {
                            html += '<td class="item" style="color:Red">' + obj.type.itemValue + '</td>';
                        } else {
                            html += '<td class="item" style="color:Blue">' + obj.type.itemValue + '</td>';
                        }
                    }
                    html += '<td>';
                    html += '<img class="item toobarimg showreason" src="../../icon/unfold.png" onclick="expandlist( ' + i + ', this)"></img>';
                    html += '</td>';
                    html += '</tr>';
                    html += '</table>';
                    html += '</div>';
                    html += '</div>';
                    html += '<li class="reason" data-click="0" style="height:50px;">'
                    html += '<table >';
                    html += '<tr>';
                    html += '<td style="width:40%">' + obj.course.name + '</td>';
                    html += '<td style="width:33%">' + obj.teacher.fullname + '</td>';
                    if (obj.allCome == 0) {
                        html += ' <td class="aui-col-xs-3" style="width:33%;left:60%;" onclick="callphone(' + k + ')">';
                        html += ' <img src="../../icon/phone.png" alt="">';
                        html += ' </td>';
                        k++;
                    } else {
                        html += '<td class="aui-col-xs-3" style="width:33%;left:60%;"></td>';
                    }
                    html += '</tr>'
                    html += '</table>';
                    html += '</li>';
                    html += '</li>';
                    $api.append(ul, html);
                    $("li"+i).on("taphold", function() {
                        $(this).hide();
                    });
                }
            }
        } else {
            alert(JSON.stringify(err));
        }
    });
}

function callphone(k) {
    api.confirm({
        title: '联系该学生',
        msg: '是否要拨打电话至该学生？',
        buttons: ["取消", "确定"]
    }, function(ret, err) {
        if (ret.buttonIndex == 1) {

        } else {
            api.call({
                type: 'tel_prompt',
                number: phones[k]
            });
        }
    });
}

function openDatepicker(gd) {
    var Dt = gettoday();
    api.openPicker({
        type: 'date',
        date: Dt,
        title: '开始日期'
    }, function(ret, err) {
        if (ret) {
            beginday = ret.year + "-" + ret.month + "-"+ ret.day;
            api.openPicker({
                type: 'date',
                date: Dt,
                title: '截止日期'
            }, function(ret, err) {
                if (ret) {
                    endday = ret.year + "-" + ret.month + "-"+ ret.day;
                    refreshlist(gd,beginday,endday);
                } else {
                    alert(JSON.stringify(err));
                }
            });
        } else {
            alert(JSON.stringify(err));
        }
    });
}

//长按删除操作
var timeOutEvent = 0; //定时器
//开始按
function gtouchstart(ele) {
    var e = window.event
    var x = e.touches[0].pageX,
        y = e.touches[0].pageY
    timeOutEvent = setTimeout("longPress(" + x + "," + y + ")", 5000); //这里设置定时器，定义长按500毫秒触发长按事件，时间可以自己改，个人感觉500毫秒非常合适
    return false;
};
//手释放，如果在500毫秒内就释放，则取消长按事件，此时可以执行onclick应该执行的事件
function gtouchend() {
    //timeOutEvent=0;
    clearTimeout(timeOutEvent); //清除定时器
    if (timeOutEvent != 0) {
        //这里写要执行的内容（尤如onclick事件）
    }
    return false;
};
//如果手指有移动，则取消所有事件，此时说明用户只是要移动而不是长按
function gtouchmove() {
    //timeOutEvent=0;
    clearTimeout(timeOutEvent); //清除定时器
    timeOutEvent = 0;

};
//真正长按后应该执行的内容
function longPress(x, y) {
    timeOutEvent = 0;
    $api.css($api.dom('.shadow'), 'display:inline-block;top:' + y + 'px;left:' + x + 'px;');
    //执行长按要执行的内容，如弹出菜单
    //alert("长按事件触发发");
    api.confirm({
        title: '删除',
        msg: '是否删除本条消息？',
        buttons: ['确定', '取消']
    }, function(ret, err) {
        if (ret.buttonIndex == 1) {
            alert("删除成功！")
        } else {
            alert("已取消删除");
        }
    });
}
